<script setup>
import ShopInfo from '@/components/shop/ShopInfo.vue'
import Product from '@/components/Product/Product.vue'
import CartFooter from '@/components/cart/CartFooter.vue'
import useShopStore from '@/stores/shop.js'
import {useRoute,useRouter} from 'vue-router'
import { computed,ref } from 'vue';


const shopStore = useShopStore()
const route=useRoute()
const router = useRouter()
const shopInfo= shopStore.nearByList.find(item =>item.id==route.query.id)


const curCate = ref(0)
const getCurCate = (id)=>{
    curCate.value=id
}

const productList = computed(()=>{
    let arr=[]
    if(curCate.value===0){
        shopInfo.category.forEach(item =>{
            arr = [...arr,...item.list]
        })
        return arr
    }else{
        arr = shopInfo.category.find(item => item.id== curCate.value)
        return arr.list
    }
})
</script>

<template>
    <div class="shop">
        <!-- 搜索 -->
        <div class="search">
            <span class="iconfont search_back" @click="router.go(-1)" >&#xe697;</span>
            <div class="search_content">
                <span class="iconfont search_content_icon">&#xe632;</span>
                <input type="text" class="search_content_input" placeholder="请输入商品名称">
            </div>
        </div>
        <!-- 商店信息 -->
        <div class="shop_info">
            <ShopInfo :shopInfo="shopInfo"></ShopInfo>
        </div>
        <!-- 内容 -->
        <div class="content">
            <div class="category">
                <div class="category_item" :class="{'category_item--active': curCate===0}" @click="getCurCate(0)" >全部商品</div>
                <div class="category_item"  v-for="item in shopInfo.category" :key="item.id" @click="getCurCate(item.id)" :class="{'category_item--active' : curCate === item.id}" >{{ item.name }}</div>
            </div>
            <div class="product">
                <div v-for="item in productList" :key="item.id">
                    <Product :productInfo="item" :shopName="shopInfo.name"></Product>
                </div>
            </div>
        </div>
        <!-- 购物车底部 -->
        <CartFooter :shopName="shopInfo.name"></CartFooter>
    </div>
</template>

<style lang="scss" scoped>
@import "@/style/viriables.scss";
.shop {
    
    .search {
        margin: .18rem;
        display: flex;
        align-items: center;

        &_back {
            font-size: .24rem;
            margin-right: .08rem;
            color: #b6b6b6;
        }
        &_content {
            flex: 1;
            height: .32rem;
            background-color: #f5f5f5;
            border-radius: .16rem;
            display: flex;
            align-items: center;

            &_icon {
                color: #b6b6b6;
                margin: 0 .12rem;
            }
            &_input {
                border: none;
                outline: none;
                background: none;
                width: 100%;
                height: .32rem;
            }
        }
    }
    .shop_info {
        margin: 0 .18rem;
    }

    .content {
        position: absolute;
        left: 0;
        right: 0;
        top: 1.7rem;
        bottom: .5rem;
        display: flex;

        .category {
            overflow-y: scroll;
            width: .78rem;
            background-color: #f5f5f5;

            &_item {
                line-height: .4rem;
                font-size: .14rem;
                text-align: center;
                color: #333;
                &--active {
                    background-color: #FFF;
                }
            }
        }
        .product {
            flex: 1;
            overflow-y: scroll;
        }
      }
}
</style>